package com.dykj.fireFighting.web.base.news;


import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.news.dto.AliMessageDto;
import com.dykj.fireFighting.api.base.news.entity.AliMessage;
import com.dykj.fireFighting.api.base.news.entity.NoticeNews;
import com.dykj.fireFighting.api.base.news.service.IAliMessageService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(value = "阿里短信语音接口", tags = {"阿里短信语音接口"})
@RestController
@RequestMapping("/aliMessage")
public class AliMessageController {

    /**
     * 单位（监控中心）
     */
    @DubboReference
    private IEnterpriseService enterpriseService;

    @DubboReference
    private IAliMessageService aliMessageService;

    /**
     * 查询信息
     */
    @AutoLog(value = "查询信息")
    @ApiOperation(value = "查询信息", notes = "查询信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult getFaultCount() {
        try {
            Enterprise enterprise = getEnterprise();
            int type = enterprise.getEnterpriseType();
            List<AliMessage> map = new ArrayList<>();
            // 社会单位
            if (type == 0) {
                map = aliMessageService.selectInfo(enterprise.getId() + "");
            }
            // 监控中心
            if (type == 2) {
                // 获取所有单位
                List<Enterprise> lists = enterpriseService.queryUnitSelect(enterprise.getId() + "");
                List<String> ids = new ArrayList<>();
                for (Enterprise list : lists ) {
                    ids.add(list.getId() + "");
                }
                map = aliMessageService.selectByCenter(ids);
            }
            return CommonResult.success(map);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return CommonResult.failed("监控中心下单位为空");
        }
    }

    /**
     * 获取单位信息
     */
    public Enterprise getEnterprise() {
        Enterprise enterprise = enterpriseService.getById(getEnterpriseId());
        return enterprise;
    }

    /**
     * 获取登录用户的单位（监控中心）id
     */
    public Long getEnterpriseId() {
        try {
            Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
            return enterpriseId;
        }catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("请登录");
        }
    }

    /**
     * 查询
     */
    @AutoLog(value = "查询信息")
    @ApiOperation(value = "查询信息", notes = "查询信息")
    @GetMapping(value = "/list")
    public CommonResult<PageVo<AliMessage>> list(AliMessageDto aliMessageDto){

        LoginUserVo user = ShiroUtil.getUser();
        Integer enterpriseType = user.getEnterpriseType();
        if(0 ==enterpriseType){
            aliMessageDto.setUnitId(user.getEnterpriseId().toString());
        }
        if(2 ==enterpriseType){
            List<Enterprise> enterprises = enterpriseService.queryUnitSelect(user.getEnterpriseId().toString());
            aliMessageDto.setUnitIdList(enterprises);
        }

        PageVo<AliMessage> page = aliMessageService.queryList(aliMessageDto);
        return CommonResult.success(page);

    }
}
